// pages/product_list/product_list.js
var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var url = ApiUrl+"index.php?act=store&op=store_goods";
var page =10;
var key= "";
// 请求数据
var  loadMore = function(that){
    key = wx.getStorageSync('key')
    if(that.data.list){
      var listBefore = that.data.list;
    }else{
      var listBefore = new Array();
    }    
    var store_id = 765;//that.data.store_id;
  var order = that.data.order ? that.data.order:"0";
    that.setData({
      showLoading:false
    }); 
   
    wx.request({
      
      url: url,
      data:{
        keyword: that.data.keyword,
        stc_id: that.data.stc_id,
        store_id: store_id,
        page: page,
        curpage: that.data.curpage,
        key: order            
      },
      method: 'GET', 
      success: function(res){
          var list = res.data;  
//console.log(res);           
          that.setData({              
            list: listBefore.concat(list.datas.goods_list),               
            showLoading: true,
            hasmore: list.hasmore,
            pagetotal: list.page_total,
            xiaoliang: that.data.xiaoliang ? that.data.xiaoliang:""
          });
          that.data.curpage++;
      },     
    });        
}  

//初始化页面
Page({  
  data:{
    showLoading:true,  
    scrollHeight:0,
    scrollTop : 0
  },
  onLoad:function(options){
    
    //这里要注意，微信的scroll-view必须要设置高度才能监听滚动事件，所以，需要在页面的onLoad事件中给scroll-view的高度赋值
    var that = this;
    var curpage = 1;
    wx.getSystemInfo({
      success:function(res){
        that.setData({
          scrollHeight:res.windowHeight,
          keyword: options.store_id,
          stc_id: options.stc_id ? options.stc_id:"",
          curpage:curpage,
          store_id:options.store_id
        });
      }
    });
      loadMore(that);      
  },
  
  //排序
  orderBy:function(event){
    var that = this;
    key = wx.getStorageSync('key')
    if (event.target.dataset.orders==2){
      that.setData({
        current: 'current',
        xiaoliang:''
      });
      }else{      
        that.setData({
          current: '',
          xiaoliang: 'current'
        });
        
      }
      that.setData({           
          key:key,
          order:event.target.dataset.order,  
          list:"",
          hasmore:true,
          curpage:1
      });   
     
      loadMore(that);   
 },

//页面滑动到底部
 bindDownLoad:function(){      
    var that = this;
    if(that.data.hasmore){
        loadMore(that);
    }else{
      return false;
    }    
 },

//商品列表页搜索
formSubmit: function(e) {      
  var that = this;      
    wx.request({
      url: ApiUrl+'index.php?act=goods&op=goods_list',
      data:{
          keyword: e.detail.value.keyword,            
      },
      method: 'GET',   
      success: function(res){
        that.setData({
          list:res.data.datas.goods_list,
        });        
      },     
  });     
},
})